{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "val zookeepers = \"X.X.X.X\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import org.locationtech.geomesa.jupyter.L\n",
    "import org.locationtech.geomesa.utils.geotools.Conversions._\n",
    "import scala.collection.JavaConversions._\n",
    "import vegas._\n",
    "import vegas.render.HTMLRenderer._\n",
    "import vegas.sparkExt._\n",
    "\n",
    "implicit val displayer: String => Unit = { s => kernel.display.content(\"text/html\", s) }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "val df = spark.read.\n",
    "  format(\"geomesa\").\n",
    "  option(\"zookeepers\", zookeepers).\n",
    "  option(\"instanceId\", \"gis\").\n",
    "  option(\"user\", \"root\").\n",
    "  option(\"password\", \"secret\").\n",
    "  option(\"tableName\", \"geomesa.gdelt\").\n",
    "  option(\"geomesa.feature\", \"gdelt\").\n",
    "  load()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df.createOrReplaceTempView(\"gdelt\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "+-------------------+---------+-------+\n",
      "|           col_name|data_type|comment|\n",
      "+-------------------+---------+-------+\n",
      "|            __fid__|   string|   null|\n",
      "|      globalEventId|   string|   null|\n",
      "|          eventCode|   string|   null|\n",
      "|      eventBaseCode|   string|   null|\n",
      "|      eventRootCode|   string|   null|\n",
      "|        isRootEvent|      int|   null|\n",
      "|         actor1Name|   string|   null|\n",
      "|         actor1Code|   string|   null|\n",
      "|  actor1CountryCode|   string|   null|\n",
      "|    actor1GroupCode|   string|   null|\n",
      "|   actor1EthnicCode|   string|   null|\n",
      "|actor1Religion1Code|   string|   null|\n",
      "|actor1Religion2Code|   string|   null|\n",
      "|         actor2Name|   string|   null|\n",
      "|         actor2Code|   string|   null|\n",
      "|  actor2CountryCode|   string|   null|\n",
      "|    actor2GroupCode|   string|   null|\n",
      "|   actor2EthnicCode|   string|   null|\n",
      "|actor2Religion1Code|   string|   null|\n",
      "|actor2Religion2Code|   string|   null|\n",
      "+-------------------+---------+-------+\n",
      "only showing top 20 rows\n",
      "\n"
     ]
    }
   ],
   "source": [
    "spark.sql(\"DESCRIBE gdelt\").show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "+-------------+--------------------+--------------------+\n",
      "|globalEventId|                geom|                 dtg|\n",
      "+-------------+--------------------+--------------------+\n",
      "|    609167622|POINT (105.667 -1...|2006-12-19 00:00:...|\n",
      "|    609167628|POINT (143.876 -9...|2006-12-19 00:00:...|\n",
      "|    609167626|POINT (143.876 -9...|2006-12-19 00:00:...|\n",
      "|    608826829|POINT (-122.637 3...|2006-12-18 00:00:...|\n",
      "|    608800220|POINT (-106.237 3...|2006-12-18 00:00:...|\n",
      "|    608837377|      POINT (-97 38)|2006-12-18 00:00:...|\n",
      "|    609122526|      POINT (-97 38)|2006-12-19 00:00:...|\n",
      "|    608695865|    POINT (-8 12.65)|2006-12-18 00:00:...|\n",
      "|    608624384|POINT (-77.0364 3...|2006-12-18 00:00:...|\n",
      "|    608870504|POINT (-74.9384 4...|2006-12-19 00:00:...|\n",
      "+-------------+--------------------+--------------------+\n",
      "\n"
     ]
    }
   ],
   "source": [
    "spark.sql(\"select globalEventId,geom,dtg from gdelt limit 10\").show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "+-------------+--------------------+--------------------+\n",
      "|globalEventId|                geom|                 dtg|\n",
      "+-------------+--------------------+--------------------+\n",
      "|    610438414|POINT (-97.0384 3...|2016-12-21 00:00:...|\n",
      "|    610438451|POINT (-97.0384 3...|2016-12-21 00:00:...|\n",
      "|    610248202|POINT (-97.0384 3...|2016-12-20 00:00:...|\n",
      "|    610438450|POINT (-97.0384 3...|2016-12-21 00:00:...|\n",
      "|    610248264|POINT (-96.8336 3...|2016-12-20 00:00:...|\n",
      "|    610231190|POINT (-96.8336 3...|2016-12-20 00:00:...|\n",
      "|    608550784|POINT (-96.8336 3...|2016-12-15 00:00:...|\n",
      "|    610231236|POINT (-96.8336 3...|2016-12-20 00:00:...|\n",
      "|    608761515|POINT (-96.2336 3...|2016-12-15 00:00:...|\n",
      "|    608760419|POINT (-96.2336 3...|2016-12-15 00:00:...|\n",
      "+-------------+--------------------+--------------------+\n",
      "\n"
     ]
    }
   ],
   "source": [
    "spark.sql(\n",
    "\"\"\"\n",
    "select globalEventId,geom,dtg \n",
    "from gdelt \n",
    "where st_contains(geom,st_geomFromWKT('POLYGON((-98 37,-95 37,-95 40,-98 40,-98 37))')) limit 10\n",
    "\"\"\"\n",
    ").show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "val ds = org.geotools.data.DataStoreFinder.getDataStore(\n",
    "Map(\n",
    "\"zookeepers\" -> zookeepers,\n",
    "\"instanceId\" -> \"gis\",\n",
    "\"user\" -> \"root\",\n",
    "\"password\" -> \"secret\",\n",
    "\"tableName\" -> \"geomesa.gdelt\"))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "val fs = ds.getFeatureSource(\"gdelt\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "val sf = fs.getFeatures().features.take(5).toList.map(_.getDefaultGeometry.asInstanceOf[com.vividsolutions.jts.geom.Point]).map { p => L.Circle(p.getX, p.getY, 50, L.StyleOptions())}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<iframe id=\"ehvTM\" sandbox=\"allow-scripts allow-same-origin\" style=\"border:none;width=100%;height:520px\" srcdoc=\"\n",
       "&lt;html&gt;\n",
       "  &lt;head&gt;\n",
       "    &lt;link rel=&quot;stylesheet&quot; href=&quot;js/leaflet.css&quot; /&gt;\n",
       "    &lt;script src=&quot;js/leaflet.js&quot;&gt;&lt;/script&gt;\n",
       "    &lt;script src=&quot;js/leaflet.wms.js&quot;&gt;&lt;/script&gt;\n",
       "    &lt;script src=&quot;js/countries.geo.json&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;\n",
       "  &lt;/head&gt;\n",
       "  &lt;body&gt;\n",
       "    &lt;div id='map' style=&quot;width:100%;height:500px&quot;&gt;&lt;/div&gt;\n",
       "    &lt;script&gt;\n",
       "      // Initialize the Base Layer... Loaded from GeoJson\n",
       "      var basestyle = {&quot;color&quot;: &quot;#717171&quot;, &quot;weight&quot;: 2, &quot;opacity&quot;: 1.0};\n",
       "      var base = L.geoJson(worldMap, basestyle);\n",
       "\n",
       "      //'map' is the id of the map\n",
       "      var map = L.map('map', {\n",
       "        crs: L.CRS.EPSG4326,\n",
       "        center: [0.0, 0.0],\n",
       "        zoom: 8\n",
       "      });\n",
       "\n",
       "      map.addLayer(base);\n",
       "      \n",
       "L.circle([38.2324, -122.637], 50.0, \n",
       "{\n",
       "  color: '#000000',\n",
       "  fillColor: '#327A66',\n",
       "  fillOpacity: '0.75'\n",
       "}\n",
       "       ).addTo(map);\n",
       "       \n",
       "\n",
       "L.circle([37.7749, -122.419], 50.0, \n",
       "{\n",
       "  color: '#000000',\n",
       "  fillColor: '#327A66',\n",
       "  fillOpacity: '0.75'\n",
       "}\n",
       "       ).addTo(map);\n",
       "       \n",
       "\n",
       "L.circle([37.7749, -122.419], 50.0, \n",
       "{\n",
       "  color: '#000000',\n",
       "  fillColor: '#327A66',\n",
       "  fillOpacity: '0.75'\n",
       "}\n",
       "       ).addTo(map);\n",
       "       \n",
       "\n",
       "L.circle([37.8044, -122.271], 50.0, \n",
       "{\n",
       "  color: '#000000',\n",
       "  fillColor: '#327A66',\n",
       "  fillOpacity: '0.75'\n",
       "}\n",
       "       ).addTo(map);\n",
       "       \n",
       "\n",
       "L.circle([37.8044, -122.271], 50.0, \n",
       "{\n",
       "  color: '#000000',\n",
       "  fillColor: '#327A66',\n",
       "  fillOpacity: '0.75'\n",
       "}\n",
       "       ).addTo(map);\n",
       "       \n",
       "\n",
       "    &lt;/script&gt;\n",
       "  &lt;/body&gt;\n",
       "&lt;/html&gt;\n",
       "     \"></iframe>\n",
       "<script>\n",
       "  if(typeof resizeIFrame != 'function') {\n",
       "    function resizeIFrame(el, k) {\n",
       "      el.style.height = el.contentWindow.document.body.scrollHeight + 'px';\n",
       "      el.style.width = '100%';\n",
       "      if(k<=3) { setTimeout(function() { resizeIFrame(el, k+1)}, 1000) };\n",
       "    }\n",
       "  }\n",
       "  $().ready(function() {\n",
       "    resizeIFrame($('#ehvTM').get(0), 1);\n",
       "  });\n",
       "</script>\n",
       "     "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "L.show(sf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "val df = spark.sql(\"select eventCode,count(eventCode) as count from gdelt group by eventCode order by count desc limit 20\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\r",
      "                                                                                \r",
      "+---------+------+\n",
      "|eventCode| count|\n",
      "+---------+------+\n",
      "|      010|100980|\n",
      "|      042| 79710|\n",
      "|      043| 76407|\n",
      "|      020| 71148|\n",
      "|      051| 68348|\n",
      "|      040| 65268|\n",
      "|      190| 48817|\n",
      "|      036| 42550|\n",
      "|      173| 40695|\n",
      "|      046| 33845|\n",
      "|      090| 26066|\n",
      "|      013| 25580|\n",
      "|      111| 25370|\n",
      "|      120| 25263|\n",
      "|      112| 23002|\n",
      "|      012| 22725|\n",
      "|      193| 22351|\n",
      "|      030| 21840|\n",
      "|      110| 21369|\n",
      "|      071| 19856|\n",
      "+---------+------+\n",
      "\n"
     ]
    }
   ],
   "source": [
    "df.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "  <iframe id=\"frame-vegas-c056b649-3ea3-430b-92c0-ea3add9599ec\" sandbox=\"allow-scripts allow-same-origin\" style=\"border: none; width: 100%\" srcdoc=\"&lt;html&gt;\n",
       "  &lt;head&gt;\n",
       "    &lt;script src=&quot;http://cdn.jsdelivr.net/webjars/org.webjars.bower/d3/3.5.17/d3.min.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;\n",
       "&lt;script src=&quot;http://cdn.jsdelivr.net/webjars/org.webjars.bower/vega/2.6.3/vega.min.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;\n",
       "&lt;script src=&quot;http://cdn.jsdelivr.net/webjars/org.webjars.bower/vega-lite/1.2.0/vega-lite.min.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;\n",
       "&lt;script src=&quot;https://vega.github.io/vega-editor/vendor/vega-embed.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;\n",
       "  &lt;/head&gt;\n",
       "  &lt;body&gt;\n",
       " &lt;script&gt;\n",
       "   var embedSpec = {\n",
       "     mode: &quot;vega-lite&quot;,\n",
       "     spec: {\n",
       "  &quot;mark&quot; : &quot;bar&quot;,\n",
       "  &quot;encoding&quot; : {\n",
       "    &quot;x&quot; : {\n",
       "      &quot;field&quot; : &quot;eventCode&quot;,\n",
       "      &quot;type&quot; : &quot;nominal&quot;\n",
       "    },\n",
       "    &quot;y&quot; : {\n",
       "      &quot;field&quot; : &quot;count&quot;,\n",
       "      &quot;type&quot; : &quot;quantitative&quot;\n",
       "    }\n",
       "  },\n",
       "  &quot;description&quot; : &quot;Event Code&quot;,\n",
       "  &quot;data&quot; : {\n",
       "    &quot;values&quot; : [\n",
       "      {\n",
       "        &quot;eventCode&quot; : &quot;010&quot;,\n",
       "        &quot;count&quot; : 100980\n",
       "      },\n",
       "      {\n",
       "        &quot;eventCode&quot; : &quot;042&quot;,\n",
       "        &quot;count&quot; : 79710\n",
       "      },\n",
       "      {\n",
       "        &quot;eventCode&quot; : &quot;043&quot;,\n",
       "        &quot;count&quot; : 76407\n",
       "      },\n",
       "      {\n",
       "        &quot;eventCode&quot; : &quot;020&quot;,\n",
       "        &quot;count&quot; : 71148\n",
       "      },\n",
       "      {\n",
       "        &quot;eventCode&quot; : &quot;051&quot;,\n",
       "        &quot;count&quot; : 68348\n",
       "      },\n",
       "      {\n",
       "        &quot;eventCode&quot; : &quot;040&quot;,\n",
       "        &quot;count&quot; : 65268\n",
       "      },\n",
       "      {\n",
       "        &quot;eventCode&quot; : &quot;190&quot;,\n",
       "        &quot;count&quot; : 48817\n",
       "      },\n",
       "      {\n",
       "        &quot;eventCode&quot; : &quot;036&quot;,\n",
       "        &quot;count&quot; : 42550\n",
       "      },\n",
       "      {\n",
       "        &quot;eventCode&quot; : &quot;173&quot;,\n",
       "        &quot;count&quot; : 40695\n",
       "      },\n",
       "      {\n",
       "        &quot;eventCode&quot; : &quot;046&quot;,\n",
       "        &quot;count&quot; : 33845\n",
       "      },\n",
       "      {\n",
       "        &quot;eventCode&quot; : &quot;090&quot;,\n",
       "        &quot;count&quot; : 26066\n",
       "      },\n",
       "      {\n",
       "        &quot;eventCode&quot; : &quot;013&quot;,\n",
       "        &quot;count&quot; : 25580\n",
       "      },\n",
       "      {\n",
       "        &quot;eventCode&quot; : &quot;111&quot;,\n",
       "        &quot;count&quot; : 25370\n",
       "      },\n",
       "      {\n",
       "        &quot;eventCode&quot; : &quot;120&quot;,\n",
       "        &quot;count&quot; : 25263\n",
       "      },\n",
       "      {\n",
       "        &quot;eventCode&quot; : &quot;112&quot;,\n",
       "        &quot;count&quot; : 23002\n",
       "      },\n",
       "      {\n",
       "        &quot;eventCode&quot; : &quot;012&quot;,\n",
       "        &quot;count&quot; : 22725\n",
       "      },\n",
       "      {\n",
       "        &quot;eventCode&quot; : &quot;193&quot;,\n",
       "        &quot;count&quot; : 22351\n",
       "      },\n",
       "      {\n",
       "        &quot;eventCode&quot; : &quot;030&quot;,\n",
       "        &quot;count&quot; : 21840\n",
       "      },\n",
       "      {\n",
       "        &quot;eventCode&quot; : &quot;110&quot;,\n",
       "        &quot;count&quot; : 21369\n",
       "      },\n",
       "      {\n",
       "        &quot;eventCode&quot; : &quot;071&quot;,\n",
       "        &quot;count&quot; : 19856\n",
       "      }\n",
       "    ]\n",
       "  }\n",
       "}\n",
       "   }\n",
       "   vg.embed(&quot;#vegas-c056b649-3ea3-430b-92c0-ea3add9599ec&quot;, embedSpec, function(error, result) {});\n",
       " &lt;/script&gt;\n",
       " &lt;div id='vegas-c056b649-3ea3-430b-92c0-ea3add9599ec'&gt;&lt;/div&gt;\n",
       "    &lt;/body&gt;\n",
       "&lt;/html&gt;\"></iframe>\n",
       "  <script>\n",
       "    if (typeof resizeIFrame != 'function') {\n",
       "      function resizeIFrame(el, k) {\n",
       "        $(el.contentWindow.document).ready(function() {\n",
       "          el.style.height = el.contentWindow.document.body.scrollHeight + 'px';\n",
       "        });\n",
       "        if (k <= 10) { setTimeout(function() { resizeIFrame(el, k+1) }, 1000 + (k * 250)) };\n",
       "      }\n",
       "    }\n",
       "    $().ready( function() { resizeIFrame($('#frame-vegas-c056b649-3ea3-430b-92c0-ea3add9599ec').get(0), 1); });\n",
       "  </script>\n",
       "    "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "Vegas(\"Event Code\").\n",
    "  withDataFrame(df).\n",
    "  encodeX(\"eventCode\", Nom).\n",
    "  encodeY(\"count\", Quant).\n",
    "  mark(Bar).\n",
    "  show(displayer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "GeoMesa Spark - Scala",
   "language": "scala",
   "name": "geomesa_spark_scala"
  },
  "language_info": {
   "file_extension": ".scala",
   "name": "scala",
   "version": "2.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
